<?php

/* @var $this PostController */
/* @var $model Post */
$this->breadcrumbs = array(
    'Posts' => array('index'),
    'Manage',
);
$this->menu = array(
    array('label' => 'Create Post', 'url' => array('create')),
);
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();

	return false;
});

$('.search-form form').submit(function(){
	$('#post-grid').yiiGridView('update', {
		data: $(this).serialize()
	});

	return false;
});
");
?>
    <div class="content-box-header">
    <h3>Quản lý Bài viết</h3>
    <ul class="content-box-tabs">
        <li><a href="#tab1" class="default-tab">Table</a></li>
        <!-- href must be unique and match the id of target div -->
        <li><a href="#tab2">Forms</a></li>
    </ul>
    <div class="clear"></div>
</div>
    <!-- End .content-box-header -->
    <div class="content-box-content">
    <div id="response"></div>
    <div class="tab-content default-tab" id="tab1">

<?php $grid = $this->widget('zii.widgets.grid.CGridView', array(
    'id'           => 'post-grid',
    'dataProvider' => $model->search(),
    'filter'       => $model,
    'selectableRows' => 2,
    'selectionChanged' => "updateSelectors",
    'enablePagination' => false,
    'afterAjaxUpdate' => 'js:function(){
        $(".changeValue").bind("click change", function() {
            var orderId = $(this).attr("id").replace("changeValue_", "");
            var orderValue = $(this).val();
            var orderAttribute = $(this).attr("class").replace("changeValue ", "");
            var gridName = "' . Yii::app()->controller->id .'";
            var modelName = "' . ucfirst(Yii::app()->controller->id) .'";

            switch (orderAttribute) {
                case "page":
                    orderValue = $(this).is(":checked") == true ? 1 : 0;
                    break;
                case "status":
                    orderValue = $(this).attr("alt") == "active" ? 1 : 2;
                    break;
            }

            $.ajax({
                url:"' . Helper::url('/Admin/default/ajaxUpdate') . '",
                type: "post",
                data: { "id": orderId, "value": orderValue, "attr": orderAttribute, "model": modelName },
                success: function(){
                    $.fn.yiiGridView.update(gridName + "-grid");
                }
            });

            return false;
        })

        $("#post-grid tr").each(function(){
            order = $(this).find("td:first input").val();
            $(this).attr("id","post_"+order);
            $(this).css("cursor", "pointer")
        });

        $("tbody").sortable({ opacity: 0.8, cursor: "move", update: function() {
                var order = $(this).sortable("serialize") + "&update=update";
                $.post("' . Helper::url("/Admin/post/admin") . '", order, function(theResponse){
                    $.fn.yiiGridView.update("post-grid");
                });

                return false;
            }
        });
    }',
    'columns'      => array(
        array(
            'class' => 'CCheckBoxColumn',
            'value' => '$data->id',
            'htmlOptions' => array('width' => '3%', 'class' => 'check-all'),
        ),
        'id',
        'title',
        'image',
        array(
            'name'        => 'status',
            'value'       => '"<a href=\"#\" class=\"set-status\" id=\"".$data->id."\">".($data->status == 1 ? CHtml::image(Yii::app()->theme->baseUrl."/images/log_severity1.gif","active") : CHtml::image(Yii::app()->theme->baseUrl."/images/delicon.gif","inactive"))."</a>"',
            'type'        => 'raw',
            'filter'      => Lookup::items('status'),
            'htmlOptions' => array('style' => 'text-align: center'),
        ),
        'tags',
        'priority',
        array(
            'name' => 'page',
            'value' => 'CHtml::checkBox("changeValue[$data->id]", $data->page, array("style" => "width: 70px; padding: 2px 5px;", "class" => "changeValue page"))',
            'type'  => 'raw',
        ),
        'ordering',
        'owner_id',
        /*
        'alias',
        'info',
        'content',
        'create_date',
        'update_date',
        'publish_date',
        'lang',
        'user_id',
        */
        array(
            'class' => 'CButtonColumn',
        ),
    ),
)); ?>
    </div>
</div>

<?php
$script = '
    $(".changeValue").bind("click change", function() {
        var orderId = $(this).attr("id").replace("changeValue_", "");
        var orderValue = $(this).val();
        var orderAttribute = $(this).attr("class").replace("changeValue ", "");
        var gridName = "' . Yii::app()->controller->id .'";
        var modelName = "' . ucfirst(Yii::app()->controller->id) .'";

        switch (orderAttribute) {
            case "page":
                orderValue = $(this).is(":checked") == true ? 1 : 0;
                break;
            case "status":
                orderValue = $(this).attr("alt") == "active" ? 1 : 2;
                break;
        }

        $.ajax({
            url:"' . Helper::url('/Admin/default/ajaxUpdate') . '",
            type: "post",
            data: { "id": orderId, "value": orderValue, "attr": orderAttribute, "model": modelName },
            success: function(){
                $.fn.yiiGridView.update(gridName + "-grid");
            }
        });

        return false;
    })

    $("#post-grid tr").each(function(){
        order = $(this).find("td:first input").val();
        $(this).attr("id","post_"+order);
        $(this).css("cursor", "pointer")
    });

    $("tbody").sortable({ opacity: 0.8, cursor: "move", update: function() {
            var order = $(this).sortable("serialize") + "&update=update";
            $.post("' . Helper::url("/Admin/post/admin") . '", order, function(theResponse){
                $.fn.yiiGridView.update("post-grid");
            });

            return false;
        }
    });
';
Helper::cs()->registerScript('set-status', $script, CClientScript::POS_END);

if ($grid->dataProvider->ItemCount) {
    $this->menu[] = array(
        'label' => 'Delete selected items', 'url' => array('delete'),
        'linkOptions' => array('onclick' => 'return multipleDelete("post-grid",this.href)')
    );
}

Helper::cs()->registerScriptFile(Helper::themeUrl() . '/js/multi-delete.js', CClientScript::POS_END);


